import request from '@/utils/request'

// 查询商品列表
export function listGoods(query) {
  return request({
    url: '/procurementmanagement/goods/list',
    method: 'get',
    params: query
  })
}

// 查询商品详细
export function getGoods(id) {
  return request({
    url: '/procurementmanagement/goods/' + id,
    method: 'get'
  })
}

// 新增商品
export function addGoods(data) {
  return request({
    url: '/procurementmanagement/goods',
    method: 'post',
    data: data
  }).then(response => {
    // 统一响应结构处理
    if (response.data) {
      return response.data; // 如果后端使用统一响应结构
    } else if (response.id) {
      return response; // 如果后端直接返回商品对象
    } else {
      throw new Error('无效的响应格式');
    }
  });
}

// 修改商品
export function updateGoods(data) {
  return request({
    url: '/procurementmanagement/goods',
    method: 'put',
    data: data
  })
}

// 删除商品
export function delGoods(id) {
  return request({
    url: '/procurementmanagement/goods/' + id,
    method: 'delete'
  })
}
